package ink.fujisann.learning.base.file;

import ink.fujisann.learning.base.FileConst;
import ink.fujisann.learning.stock.pojo.SysFile;
import ink.fujisann.learning.stock.repository.SysFileRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/file")
@Api(tags = {"005-新的文件服务"})
@Slf4j
public class FileController {

    private SysFileRepository sysFileRepository;

    @Autowired
    public void setSysFileRepository(SysFileRepository sysFileRepository) {
        this.sysFileRepository = sysFileRepository;
    }

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile multipartFile) {
        log.info("进入上传文件");
        String filename = multipartFile.getOriginalFilename();
        String basePath = "/usr/local/nginx/html/picblog";
        String dir = basePath + "/" + UUID.randomUUID();
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fullPath = dir + "/" + filename;
        file = new File(fullPath);
        log.info(file.getName() + " : xxx");
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            log.error("上传文件失败", e);
            return "fail";
        }
        SysFile sysFile = new SysFile();
        sysFile.setType(FileConst.PIC_BLOG_APK);
        sysFile.setFullPath(fullPath);
        sysFileRepository.save(sysFile);
        return "success";
    }

    @ApiOperation("下载picblog的安装包")
    @GetMapping("/downloadApk")
    public void downloadApk(HttpServletResponse response) {
        SysFile latest = sysFileRepository.findLatest();
        String fullPath = latest.getFullPath();
        int i = fullPath.lastIndexOf("/");
        String substring = fullPath.substring(i);
        // 设置相关格式
        response.setContentType("application/force-download");
        // 设置下载后的文件名以及header
        response.addHeader("Content-disposition", "attachment;fileName=" + substring);
        try {
            File file = new File(fullPath);
            FileInputStream fileInputStream = new FileInputStream(file);
            response.addHeader("Content-Length", String.valueOf(fileInputStream.available()));
            ServletOutputStream outputStream = response.getOutputStream();

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            outputStream.close();
            fileInputStream.close();
        } catch (IOException e) {
            log.error("文件下载异常", e);
        }
    }
}
